package tradeapi

/*
#cgo CFLAGS: -I ../ctpapi/
#cgo LDFLAGS: -L ../ctpapi/ -lTRADEBRIDGE
#cgo LDFLAGS: -L ../ctpapi/ -lthosttraderapi_se
#cgo LDFLAGS: -L ../ctpapi/ -lWinDataCollect
#include "TradeBridge.h"
#include "Common.h"
#include "stdbool.h"
#include "ThostFtdcUserApiStruct.h"
#include "string.h"

void OnFrontConnectedTrade_cgo();
void OnFrontDisconnectedTrade_cgo(int nReason);
void OnHeartBeatWarningTrade_cgo(int nTimeLapse);
void OnRspAuthenticateTrade_cgo(struct CThostFtdcRspAuthenticateField *pRspAuthenticateField,struct  CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
void OnRspUserLoginTrade_cgo(struct CThostFtdcRspUserLoginField *pRspUserLogin, struct CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
void OnRspQrySettlementInfoTrade_cgo(struct CThostFtdcSettlementInfoField *pSettlementInfo,struct  CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
*/
import "C"
import "unsafe"

type CThostFtdcReqUserLoginFieldgo struct {
	///交易日
	TradingDay string
	///经纪公司代码
	BrokerID string
	///用户代码
	UserID string
	///密码
	Password string
	///用户端产品信息
	UserProductInfo string
	///接口端产品信息
	InterfaceProductInfo string
	///协议信息
	ProtocolInfo string
	///Mac地址
	MacAddress string
	///动态密码
	OneTimePassword string
	///终端IP地址
	ClientIPAddress string
	///登录备注
	LoginRemark string
	///终端IP端口
	ClientIPPort int
}

type Tradeapi struct {
	mdapi C.pTradeAPI
}

func NewTradeapi() *Tradeapi {
	return &Tradeapi{
		C.NewTradeAPI(),
	}
}

func (x *Tradeapi) FreepTradeAPI() {
	C.FreepTradeAPI(x.mdapi)
}

func (x *Tradeapi) ConnectTradeGo(path string, addr string) {
	C.ConnectTrade(x.mdapi, C.CString(path), C.CString(addr))
}

func (x *Tradeapi) Reg_OnFrontConnectedTradeGo() {
	C.Reg_OnFrontConnectedTrade(x.mdapi, (C.OnFrontConnectedTrade)(unsafe.Pointer(C.OnFrontConnectedTrade_cgo)))
}

func (x *Tradeapi) Reg_OnFrontDisconnectedTradeGo() {
	C.Reg_OnFrontDisconnectedTrade(x.mdapi, (C.OnFrontDisconnectedTrade)(unsafe.Pointer(C.OnFrontDisconnectedTrade_cgo)))
}

func (x *Tradeapi) Reg_OnHeartBeatWarningTradeGo() {
	C.Reg_OnHeartBeatWarningTrade(x.mdapi, (C.OnHeartBeatWarningTrade)(unsafe.Pointer(C.OnHeartBeatWarningTrade_cgo)))
}
func (x *Tradeapi) InitTradeGo() {
	C.InitTrade(x.mdapi)
}
func (x *Tradeapi) GetVersionTradeMdGo() string {
	ver := C.GetVersionTrade(x.mdapi)
	return C.GoString(ver)
}
func (x *Tradeapi) JoinTradeGo() {
	C.JoinTrade(x.mdapi)
}

func (x *Tradeapi) LoginTradego(pLogin CThostFtdcReqUserLoginFieldgo, nRequestID int) {
	var cLogin C.struct_CThostFtdcReqUserLoginField

	C.strcpy(&cLogin.TradingDay[0], C.CString(pLogin.TradingDay))
	C.strcpy(&cLogin.BrokerID[0], C.CString(pLogin.BrokerID))
	C.strcpy(&cLogin.UserID[0], C.CString(pLogin.UserID))
	C.strcpy(&cLogin.Password[0], C.CString(pLogin.Password))
	C.strcpy(&cLogin.UserProductInfo[0], C.CString(pLogin.UserProductInfo))
	C.strcpy(&cLogin.InterfaceProductInfo[0], C.CString(pLogin.InterfaceProductInfo))
	C.strcpy(&cLogin.ProtocolInfo[0], C.CString(pLogin.ProtocolInfo))
	C.strcpy(&cLogin.MacAddress[0], C.CString(pLogin.MacAddress))
	C.strcpy(&cLogin.OneTimePassword[0], C.CString(pLogin.OneTimePassword))
	C.strcpy(&cLogin.ClientIPAddress[0], C.CString(pLogin.ClientIPAddress))
	C.strcpy(&cLogin.LoginRemark[0], C.CString(pLogin.LoginRemark))
	cLogin.ClientIPPort = C.int(pLogin.ClientIPPort)

	//pReqUserLoginField := C.struct_CThostFtdcReqUserLoginField{}

	C.LoginTrade(x.mdapi, &cLogin, C.int(nRequestID))

}

func (x *Tradeapi) GetTradeDayTradeGo() string {
	TradeDay := C.GetTradingDayTrade(x.mdapi)
	return C.GoString(TradeDay)
}

func (x *Tradeapi) Reg_OnRspUserLoginTradeGo() {
	C.Reg_OnRspUserLoginTrade(x.mdapi, (C.OnRspUserLoginTrade)(unsafe.Pointer(C.OnRspUserLoginTrade_cgo)))
}

func (x *Tradeapi) SendAuthGo() {
	var authfield C.struct_CThostFtdcReqAuthenticateField

	C.strcpy(&authfield.BrokerID[0], C.CString("9999"))
	C.strcpy(&authfield.UserID[0], C.CString("044491"))
	C.strcpy(&authfield.AuthCode[0], C.CString("0000000000000000"))
	C.strcpy(&authfield.AppID[0], C.CString("simnow_client_test"))

	C.SendAuth(x.mdapi, &authfield, C.int(0))
}

func (x *Tradeapi) Reg_OnRspAuthenticateTradeGo() {
	C.Reg_OnRspAuthenticateTrade(x.mdapi, (C.OnRspAuthenticateTrade)(unsafe.Pointer(C.OnRspAuthenticateTrade_cgo)))
}

func (x *Tradeapi) ReqSettlementInfoGo() {
	var settlementfield C.struct_CThostFtdcQrySettlementInfoField

	C.strcpy(&settlementfield.BrokerID[0], C.CString("9999"))
	C.strcpy(&settlementfield.InvestorID[0], C.CString("044491"))
	C.strcpy(&settlementfield.TradingDay[0], C.CString("20190904"))

	C.ReqSettlementInfo(x.mdapi, &settlementfield, C.int(0))
}

func (x *Tradeapi) Reg_OnRspQrySettlementInfoTradeGo() {
	C.Reg_OnRspQrySettlementInfoTrade(x.mdapi, (C.OnRspQrySettlementInfoTrade)(unsafe.Pointer(C.OnRspQrySettlementInfoTrade_cgo)))
}
